<template>
  <div>
    <!-- 字段类型维护 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      columnsRef="zjfieldType"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="addHandle">新增</multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visible" :title="'基本信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <childrenModel :rowData="formData" @close="emitcloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="addvisible" :title="'基本信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <addModel :rowData="formData" @close="addcloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { getList, getDetail, postSave, getDict } from '@/api/omSale/vehicleParameters/fieldType';
import { dictionary } from '@/utils/dictMixin';
import childrenModel from './childrenModel';
import addModel from './addModel';
export default {
  name: 'businessParameters',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        xhrAbort: true
      },
      fetchApi: getList,
      getDictList: [],
      formData: {},
      visible: false,
      addvisible: false,
      selectData: {}
    };
  },
  components: {
    childrenModel,
    addModel
  },
  mixins: [dictionary],
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getDict();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'SEARCH_HELPER',
          label: '字段类型',
          fieldName: 'name',
          placeholder: '请选择',
          request: {
            fetchApi: getDict,
            params: {},
            valueKey: 'type',
            textKey: 'typeName',
            change: p => {
              this.selectData = p;
            }
          }
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '字段类型',
          dataIndex: 'type',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [],
          editItems: []
        },

        {
          title: '名称',
          dataIndex: 'codeCnDesc',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '启用标记',
          dataIndex: 'status',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1003'),
          render: p => {
            return <span>{this.createDictText(p.row.status, '1003')}</span>;
          }
        },
        {
          title: '操作人',
          dataIndex: 'updatedName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '操作时间',
          dataIndex: 'updatedAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    selectChangeHandle(row) {},
    async getDict() {
      let res = await getDict();
      this.columns.find(x => x.dataIndex == 'type').filterItems = res.data.map(x => ({ text: x.typeName, value: x.type }));
      this.columns.find(x => x.dataIndex == 'type').editItems = res.data.map(x => ({ text: x.typeName, value: x.type }));
    },
    addHandle(data) {
      this.addvisible = true;
    },
    changeHandle(params) {
      params.type = this.selectData.text;
      params.typeName = this.selectData.value;
      console.log(params);
      if (params.name && params.typeName && params.name != params.typeName) {
        this.$notify({
          title: '警告',
          message: '请选择有效的字符类型',
          type: 'warning'
        });
        return;
      }
      if (params.name && !params.typeName) {
        this.$notify({
          title: '警告',
          message: '请选择有效的字符类型',
          type: 'warning'
        });
        return;
      }
      if (!params.name) {
        params.typeName = '';
        params.type = '';
      }
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    editHandle(data) {
      this.formData = data;
      this.visible = true;
    },
    addcloseHandler(val) {
      this.addvisible = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    emitcloseHandler(val) {
      this.visible = false;
      if (val) {
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    }
  }
};
</script>
